const urlPrefix = getApp().globalData.urlPrefix;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "bh_info":{},
    array: [],
    index: 0, // 默认选中第一个元素  
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBingHaoInfo(options);
    this.getProjectList(options);
  },
  getBingHaoInfo: function(option){

      this.setData({
      "bh_info":option
    })
  },
  // 获取项目列表
  getProjectList: function(option){
    const that = this;
    const openId = option.bh_belong_to;
      wx.request({
        url: urlPrefix + '/tongji/getDicList?dicName=PROJECT&openId='+openId,
        success(res){
          var data = res.data.data;
          console.log("array");
          console.log(data);
          that.setData({
            "array":data
          })
        }
      })
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  formSubmit(e) {

    this.data.bh_info.project_name = this.data.array[this.data.index]
    const bh_info = this.data.bh_info;
    bh_info.bh_id = bh_info.id;
    bh_info.user_open_id = bh_info.bh_belong_to;
    wx.request({
      url: urlPrefix + '/tongji/addRecord',
      data:bh_info,
      method:'POST',
      success:function(res){
        if(res.data.code='SUCCESS'){
          wx.showToast({
            title: '添加成功',
            icon:'success',
            mask:true,
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '/pages/tongji/tongji',
                })
              },1000)
             
            }
          })

        }
      }

    })
  },

  formReset(e) {
    this.setData({
      chosen: ''
    })
  },

})



